package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.SelectDistributionRecycleAdapter;
import com.wwwarehouse.usercenter.bean.authoritydistribute.CheckPermissionButton;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * 权限管理 -> 选择你要分配的人
 * Created by ShineF on 2017/9/25 0025.
 */

public class SelectDistributionPermissionPagerContentFragment extends BaseFragment implements View.OnClickListener, SelectDistributionRecycleAdapter.OnItemClickListener {

    private StateLayout mStateLayout;
    private RecyclerView mRvContent;
    private boolean isMeasured = false;
    private SelectDistributionRecycleAdapter mAdapter;
    private List<InviteInfoResponseBean.InviteDetailsBean> dataList;
    private static final int SIZE = 9;
    private ArrayList<InviteInfoResponseBean.InviteDetailsBean> selectList;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_select_distribution_pager_content_permission, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mStateLayout = $(R.id.state_layout);
        mRvContent = $(R.id.rv_content);
        mRvContent.addItemDecoration(new SpaceItemDecoration(10, 10));
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3));
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof SelectDistributionPermissionPagerContentFragment) {
            mActivity.setTitle(R.string.select_distribute_permission_title);
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        selectList = new ArrayList<>();
        Bundle bundle = getArguments();
        if (bundle != null) {
            int mPage = bundle.getInt("page");
            InviteInfoResponseBean bean = (InviteInfoResponseBean) bundle.getSerializable("data");
            if (bean != null && bean.getList() != null && bean.getList().size() != 0 && mPage == 1) {
                dataList = bean.getList();
                measureHeight();
                if (isMeasured && mAdapter != null) {
                    mRvContent.setAdapter(mAdapter);
                }
            } else {
                mStateLayout.showProgressView(true);
                Map<String, Object> map = new HashMap<>();
                List<String> inviteList = new ArrayList<>();
                inviteList.add("10");
                map.put("sort", "update_time desc");
                map.put("page", mPage);
                map.put("size", SIZE);
                map.put("inviteeList", inviteList);
                map.put("inviteeName", "");
                map.put("inviteeMobile", "");
                loadData(UserCenterConstant.URL_GET_INVITE_INFO, map, 1);
            }
        }
        return null;
    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                setData(JSON.parseObject(commonClass.getData().toString(), InviteInfoResponseBean.class));
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {
                mStateLayout.showContentView();
            }
        }, what);
    }

    public void setData(InviteInfoResponseBean bean) {
        if (null != bean && null != bean.getList()) {
            dataList = bean.getList();
            measureHeight();
        }
    }

    public void measureHeight() {
        ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!isMeasured) {
                    setHeight(mRvContent.getMeasuredHeight());
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public void setHeight(int height) {
        isMeasured = true;
        if (mAdapter == null && isAdded()) {
            mAdapter = new SelectDistributionRecycleAdapter(mActivity, dataList, height);
            mRvContent.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    }

    @Override
    public void ItemClickListener(int position, View view, InviteInfoResponseBean.InviteDetailsBean data) {
        EventBus.getDefault().post(new CheckPermissionButton("isSelect", data));
    }

    public boolean checkIsSelect() {
        selectList.clear();
        for (InviteInfoResponseBean.InviteDetailsBean inviteDetailsBean : dataList) {
            if (inviteDetailsBean.isSelect()) {
                selectList.add(inviteDetailsBean);
            }
        }
        return selectList.size() != 0;
    }
}
